package mystack;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Stack;

public class MyStack {
   public int[] elem;
   //有效的数据个数 也可以当做存放当前数据元数的下表
   public int useSize;
   public static final int board=10;
   public MyStack(){
       elem=new int[board];
   }
   public void push(int data){
       if (isFull()) {

           elem= Arrays.copyOf(elem,2*elem.length);
       }else {
           elem[useSize] = data;
           useSize++;
       }
   }
public boolean isFull(){
       if (useSize==elem.length){
      return true;
       }
       return false;
}
public int pop(){
       if (empty()){
           throw new EmptyStackException(" 栈为空异常");
       }
      int oldval=elem[useSize-1];
       useSize--;
       return oldval;
}
      public int peek(){
          if (empty()){
              throw new EmptyStackException(" 栈为空异常");
          }
          int oldval=elem[useSize-1];
          return oldval;
     }
public boolean empty(){
       return useSize==0;
}
public int search(int val){
    int oldval=elem[useSize-1];
       if (oldval==val){
           return useSize-1;
       }
       useSize--;
       return -1;
}
public int size(){
       return useSize;
}
    public static void main(String[] args) {
        MyStack myStack=new MyStack();
        myStack.push(10);
        myStack.push(10);
        myStack.push(10);
        myStack.push(10);
        myStack.push(10);
        myStack.push(10);
        myStack.push(10);
        System.out.println(myStack);
    }

}
